 var express = require('express');
 var router = express.Router();
 var queryWithPromise = require("../db/db").queryWithPromise

 /* GET home page. */
 router.get("/", async function(req, res, next) {
     // 前台发来的请求和传来的参数 req.query(get请求传来的参数所在方法)
     console.log(req.query)
         // 获取city表的总条数
     let sql_city_count = "select count(*) from city"
         // 向数据库查询数据
     let count_res = await queryWithPromise(sql_city_count, [])
     let total = count_res.results[0]["count(*)"]

     // 根据req.query数据即所传的数据获取分页数据
     let sql_city_list = "select * from city limit ?,?"
     let values = [parseInt(req.query.start), parseInt(req.query.pageSize)]
     let city_list_res = await queryWithPromise(sql_city_list, values)
     let data = []
         // console.log(city_list_res.results)
     city_list_res.results.map(v => {
         data.push({
             ID: v.ID,
             Name: v.Name,
             Population: v.Population
         })
     })
     res.json({
         code: 1,
         total: total,
         data: data
     })
 })
 module.exports = router